package com.listener;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;
import java.io.FileInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;

public class ProcessTestEventSubprocess {

	@Test
	public void startProcess() throws Exception {
		// 创建一个流程数据源的配置
		ProcessEngine processEngine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"diagrams/EventSubprocess.bpmn").buildProcessEngine();
		// 获取数据源连接的服务
		RepositoryService repositoryService = processEngine
				.getRepositoryService();
		// 读取配置流程配置文件对象
		Deployment deployment = repositoryService.createDeployment()
				.addClasspathResource("diagrams/EventSubprocess.bpmn")
				.deploy();
		// 发布到数据库中
		ProcessDefinition pdf = repositoryService
				.createProcessDefinitionQuery()
				.deploymentId(deployment.getId()).singleResult();

		// 开启一个服务
		RuntimeService runtimeService = processEngine.getRuntimeService();
		Map<String, Object> variableMap = new HashMap<String, Object>();
		variableMap.put("name", "Activiti");
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("EventSubprocess", variableMap);
		assertNotNull(processInstance.getId());
		System.out.println("id " + processInstance.getId() + " "
				+ processInstance.getProcessDefinitionId());

		TaskService taskService = processEngine.getTaskService();
		Task task = taskService.createTaskQuery().singleResult();
		variableMap = new HashMap<String, Object>();
		variableMap.put("pass", false);
		taskService.complete(task.getId(), variableMap);

		Task task2 = taskService.createTaskQuery().singleResult();
		assertNotNull(task2);
		taskService.complete(task2.getId());
	}
}